package sqlite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/5/13 20:08
 */
public class OpenSqlite {
    // 注册驱动地址
    private static final String CLASS_PATH ="org.sqlite.JDBC";
    // 数据库地址
    private static final String DB_PATH ="jdbc:sqlite:E:\\java softwares\\sqlitedb\\person.db";
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        // 获取链接
        Connection connection = createConnection();
        //
        String sql="insert into person('name','age') values(?,?)";
        int insert = insert(connection, sql);
        if (insert==1){
            System.out.println("插入成功");
        }else {
            System.out.println("插入失败");
        }
    }
    public static Connection createConnection() throws ClassNotFoundException, SQLException {
        // 注册驱动
        Class.forName(CLASS_PATH);
        Connection connection = DriverManager.getConnection(DB_PATH);
        return connection;
    }
    // 插入记录
    public static int insert(Connection connection,String sql) throws SQLException {
        // 获取执行平台
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //
        preparedStatement.setObject(1,"czk");
        preparedStatement.setObject(2,25);
        // 执行操作
        return preparedStatement.executeUpdate();
    }
}
